package cn.toleo.search.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.toleo.common.pojo.SearchResult;
import cn.toleo.search.service.SearchService;

/**
 * 搜索服务controller
 * @author Toleo
 *
 */

@Controller
public class SearchController {
	
	@Autowired
	private SearchService searchService;
	
	@Value("${SEARCH_RESULT_ROWS}")
	private Integer SEARCH_RESULT_ROWS;
	
	@RequestMapping("/search")
	public String search(@RequestParam("q")String queryString,@RequestParam(defaultValue="1")Integer page,Model model) throws Exception {

			
		//输入修饰		
		queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
		
		//主干
		SearchResult searchResult=searchService.search(queryString, page, SEARCH_RESULT_ROWS);
		
		//输出修饰
		model.addAttribute("query", queryString);
		model.addAttribute("totalPages", searchResult.getTotalPages());
		model.addAttribute("itemList", searchResult.getItemList());
		model.addAttribute("page", page);
		
		//结果
		return "search";
	}
}
